"use strict";


const Joi = require('joi');
const Result = require("../basics/result");
const CheckUtil = require("../utils/check");
const BasicsCtl = require("../basics/clz/basectl");
const HomeImpl = require("../service/home");
const UserApi = require("../taobao/user");
const TempApi = require("../taobao/temp");

class HomeCtl extends BasicsCtl {

  constructor(context) {
    super(context);
    this.homeImpl = new HomeImpl(this.cloud);
  }


  /**
  * 查询实例列表
  */
  async findTempList() {
    const rule = {
      templateId: Joi.string().required(),
      shopseesion: Joi.string()
    }
    CheckUtil.validate(rule, this.params);
    let { templateId, shopseesion, accessToken } = this.params;
    if (!shopseesion || shopseesion == "0") {
      shopseesion = accessToken;
    }
    return await this.homeImpl.queryTempList(templateId, shopseesion);
  }

  /**
   * 模板实例化
   */
  async instantiate() {
    const rule = {
      clients: Joi.string().required(),
      description: Joi.string().required(),
      ext_json: Joi.string().required(),
      icon: Joi.string().required(),
      template_id: Joi.string().required(),
      template_version: Joi.string().required(),
      name: Joi.string(),
      shopseesion: Joi.string()
    }
    CheckUtil.validate(rule, this.params);
    let { shopseesion, accessToken } = this.params;
    if (!shopseesion || shopseesion == "0") {
      shopseesion = accessToken;
    }
    return await TempApi.instantiate(this.cloud, shopseesion, this.params);;
  }

  /**
   * 更新实例化
   */
  async updateapp() {
    const rule = {
      clients: Joi.string().required(),
      app_id: Joi.string().required(),
      ext_json: Joi.string().required(),
      template_id: Joi.string().required(),
      template_version: Joi.string().required(),
      name: Joi.string(),
      shopseesion: Joi.string()
    }
    CheckUtil.validate(rule, this.params);
    let { shopseesion, accessToken } = this.params;
    if (!shopseesion || shopseesion == "0") {
      shopseesion = accessToken;
    }
    return await TempApi.updateapp(this.cloud, shopseesion, this.params);;
  }

  /**
   * 上线实例化应用
   */
  async onlineapp() {
    const rule = {
      clients: Joi.string().required(),
      app_id: Joi.string().required(),
      app_version: Joi.string().required(),
      template_id: Joi.string().required(),
      template_version: Joi.string().required(),
      shopseesion: Joi.string()
    }
    CheckUtil.validate(rule, this.params);
    let { shopseesion, accessToken } = this.params;
    if (!shopseesion || shopseesion == "0") {
      shopseesion = accessToken;
    }
    return await TempApi.onlineapp(this.cloud, shopseesion, this.params);;
  }
}
module.exports = HomeCtl;